package com.sam4cloud.common.core.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * @Author: Sam
 * @Date: 2022-11-09 16:44
 * @Description: json序列化配置
 */
@Configuration
public class JsonSerializerConfig {
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
        objectMapper
                .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
                //启用反序列化所需的类型信息,在属性中添加@class
                //.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
                //序列化Date日期时以timestamps输出，默认true，这里禁用
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                //对象为空时是否报错，默认true，这里禁用
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                //忽略不认识的字段 json中含pojo不存在属性时是否失败报错,默认true
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                //可解析反斜杠引用的所有字符，默认：false，不可解析
                .configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true)
                // 过滤对象的null属性. ALWAYS 默认   NON_DEFAULT 属性为默认值不序列化
                // NON_EMPTY 属性为空（””）或者为 NULL 都不序列化   NON_NULL 属性为NULL 不序列化
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                //忽略transient
                .configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true)
                //枚举转换为字符串
                .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true)
                //设置全局时区
                .setTimeZone(TimeZone.getTimeZone("GMT+8"))
                //设置全局@JsonFormat的格式pattern
                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return registerModule(objectMapper);
    }

    /**
     * 注册时间序列化模块
     */
    private static ObjectMapper registerModule(ObjectMapper objectMapper) {
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        simpleModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        simpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        simpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer((DateTimeFormatter.ofPattern("HH:mm:ss"))));
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
